{extend name="public:base"/}
{block name="main"}
<style type="text/css">
  .width-250{width:250px !important; }

</style>
<link href="__Com__/uedit/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8" src="__Com__/uedit/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__Com__/uedit/umeditor.js"></script>
<script type="text/javascript">var um = UM.getEditor('myEditors');</script>

<form class="layui-form" action="" method="POST">
    <div class="layui-form-item">
        <label class="layui-form-label">选择分类</label>
        <div class="layui-input-block width-350"  >
           <select name="cateid" lay-verify="required" lay-filter="select">
            <option value="">请选择分类</option>
              {foreach $catelist as $k=>$v}
              <option value="{$v['cateid']}" {if condition="$info['cateid'] eq $v['cateid'] "}selected="selected"{/if}>{$v['catename']}</option>
              {/foreach}
          </select>  
        </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">文章标题</label>
      <div class="layui-input-block width-350">
      	<input type="hidden" name="newsid" value="{$info['newsid']}">
        <input type="text" name="title" required="" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input" value="{$info['title']}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">文章关键字</label>
      <div class="layui-input-block width-350">
        <input type="text" name="keyword"  placeholder="请输入文章关键字" autocomplete="off" class="layui-input"  value="{$info['keyword']}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">文章描述</label>
      <div class="layui-input-block width-350">
        <input type="text" name="description"  placeholder="请输入文章描述" autocomplete="off" class="layui-input"  value="{$info['description']}">
      </div>
    </div>
    
    <div class="layui-form-item">
      <label class="layui-form-label">封面图片</label>
      <div class="layui-input-inline" style="width:350px;">
        <input type="text" name="thumb"  placeholder="图片地址" autocomplete="off" class="layui-input"  value="{$info['thumb']}">
      </div>
      <div class="layui-form-mid layui-word-aux" style="padding-top: 0px !important; ">
        {notempty name="info['thumb']"}
        <img src="{$info['thumb']}" id="thumb" class="content-photos">
        {/notempty}
        <button type="button" class="layui-btn layui-btn-small layui-btn-normal thumbupload" lay-data="{data:{type:'image'},accept:'images'}" data-name="thumb" style="margin-left:10px;">
          <i class="layui-icon">&#xe64a;</i>上传图片
        </button>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">状态</label>
      <div class="layui-input-block width-350">
        <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" lay-filter="switchTest" value="1" {if condition="($info['status'] eq '1') OR ($info['status'] neq '0')"}checked=''{/if}>
        <div class="layui-unselect layui-form-switch" lay-skin="_switch">
        	<em>OFF</em><i></i>
        </div>
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">文章摘要</label>
        <div class="layui-input-block">
            <textarea name="abstract" placeholder="请输入文章摘要" class="layui-textarea">{$info['abstract']}</textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
		    <label class="layui-form-label">文章内容</label>
		    <div class="layui-input-block">
        <script type="text/plain" id="myEditors" name="content" style="height:240px;width:100%;">{$info['content']|htmlspecialchars_decode}</script>
		    <p><input type="checkbox"  title="设定内容第一张图片为封面" lay-skin="primary" lay-filter="gethumb"></p>
        </div>
		</div>
    <div class="layui-form-item">
    <label class="layui-form-label">跳转链接</label>
    <div class="layui-input-block width-350">
      <input type="text" name="url" requplaceholder="请输入跳转链接" autocomplete="off" class="layui-input" value="{$info['url']}">
    </div>
  </div>
    <div class="layui-form-item">
      <label class="layui-form-label">相册</label>
      <div class="layui-form-mid layui-word-aux" style="padding-top: 0px !important; ">
        <button type="button" class="layui-btn layui-btn-small layui-btn-normal photosload" lay-data="{data:{type:'image'},accept:'images'}" data-name="photos">
          <i class="layui-icon">&#xe60d;</i>上传相册
        </button>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">附件</label>
      <div class="layui-form-mid layui-word-aux" style="padding-top: 0px !important; ">
        <button  type="button" class="layui-btn layui-btn-small layui-btn-normal filesupload" lay-data="{data:{type:'file'},accept:'file'}" data-name="file">
          <i class="layui-icon">&#xe61d;</i>上传附件
        </button>
        
      </div>
      <div id="filedata" >
          {notempty name="info['files']"}
          <input type="hidden" name="files" value="{$info['files']['src']}">
          <a href="{$info['files']['src']}" target="_blank">{$info['files']['name']}</a>&nbsp;&nbsp;
          <a href="javascript:delfiledata();">删除</a></div>
          {/notempty}
      </div>
    </div>
    <div class="layui-form-item" id="PhotosDom">
    {notempty name="info['photos']"}
        {foreach $info['photos']['name'] as $k=>$v}

          <div class="layui-form-item">
              <label class="layui-form-label"></label>
              <div class="layui-input-inline" style="width: 100px;">
              <input type="text" name="photos[name][]" lay-verify="required" placeholder="图片名称" autocomplete="off" class="layui-input" placeholder="文件名称" value="{$info['photos']['name'][$k]}">
              <input type="hidden" name="photos[path][]" placeholder="图片地址"></div>
              <img src="{$info['photos']['path'][$k]}" class="content-photos" value="{$info['photos']['path'][$k]}">
              <button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delimg(this)">删除</button>
          </div>

        {/foreach}
    {/notempty}
       
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
</form>

<script type="text/javascript">
	layui.use(['form','layedit','upload'], function(){
	  var form = layui.form
	  ,layedit = layui.layedit
    ,upload  = layui.upload;

	  //var editIndex = layedit.build('content');

    /*缩略图上传*/
    upload.render({
      elem: '.thumbupload' //绑定元素
      ,url: '{:Url('common/fileupload',array('other'=>'newinfo'))}' //上传接口
      ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。

        layer.load(); //上传loading
      }
      ,done: function(res,index){ 

          layer.closeAll('loading');

          if(res.status=='1'){ 

              $("#thumb").remove();

              var item = this.item;

              var dom=item.attr('data-name');
              layui.$("input[name='"+dom+"']").val(res.path);
              $(item).before('<img src="'+res.path+'" id="thumb" class="content-photos">');

          }else{
            alert("上传失败,请重试！");
          }
      }
      ,error: function(){
          layer.closeAll('loading');
          alert("上传失败,请重试！");
        //请求异常回调
      }
      ,field:'file' //字段名

    });
    /*相册*/
    upload.render({
      elem: '.photosload' //绑定元素
      ,url: '{:Url('common/fileupload',array('other'=>'newinfo'))}' //上传接口
      ,multiple: true  //多文件上传
      ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
        //console.log(obj);
        layer.load(); //上传loading
      }
      ,done: function(res,index){ 

          layer.closeAll('loading');

          if(res.status=='1'){
            
              var item = this.item;

             /* var html="<div class='layui-inline'><label class='layui-form-label'></label><div class='layui-input-inline' style='width: 100px;'><input type='text' name='photos[name][]' lay-verify='required' placeholder='图片名称' autocomplete='off' class='layui-input' placeholder='文件名称' value="'+res.filename+'"><input type='hidden' name='photos[file][]' value="'+res.path+'" placeholder='图片地址'></div><img src="'+res.path+'" class='content-photos'><button type='button' class='layui-btn layui-btn-danger layui-btn-xs' onclick='delimg(this)'>删除</button></div><br/>";*/

              var html='<div class="layui-form-item"><label class="layui-form-label"></label><div class="layui-input-inline" style="width: 100px;"><input type="text" name="photos[name][]" lay-verify="required" placeholder="图片名称" autocomplete="off" class="layui-input" placeholder="文件名称" value="'+res.filename+'"><input type="hidden" name="photos[path][]" value="'+res.path+'" placeholder="图片地址"></div><img src="'+res.path+'" class="content-photos"><button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delimg(this)">删除</button></div><br/>';

              $("#PhotosDom").append(html);

              /*var dom=item.attr('data-name');
              layui.$("input[name='"+dom+"']").val(res.path);*/

          }else{
            alert("上传失败,请重试！");
          }
      }
      ,error: function(){
          layer.closeAll('loading');
          alert("上传失败,请重试！");
        //请求异常回调
      }
      ,field:'file' //字段名

    });

    /*附件*/
    upload.render({
      elem: '.filesupload' //绑定元素
      ,url: '{:Url('common/fileupload')}' //上传接口
      ,multiple: false  //多文件上传
      ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
        //console.log(obj);
        layer.load(); //上传loading
      }
      ,done: function(res,index){ 

          layer.closeAll('loading');

          if(res.status=='1'){

              var html='<input type="hidden" name="files[src]" value="'+res.path+'"><input type="hidden" name="files[name]" value="'+res.filename+'"><a href="'+res.path+'" target="_blank">'+res.filename+'</a>&nbsp;&nbsp;<a href="javascript:delfiledata();">删除</a>';

              $("#filedata").html(html);


          }else{
            alert("上传失败,请重试！");
          }
      }
      ,error: function(){
          layer.closeAll('loading');
          alert("上传失败,请重试！");
        //请求异常回调
      }
      ,field:'file' //字段名

    });


    {notempty name="info"}
    
      layui.$("select[name='cateid']").val({$info['cateid']});
      layui.form.render('select')
    {/notempty}

	  form.verify({
	    title: function(value){
	      if(value.length < 5){
	        return '标题至少得50个字符啊';
	      }
	    }
	  });

    form.on('checkbox(gethumb)', function(data){
      if(data.elem.checked==true){
          var text=UM.getEditor('myEditors').getPlainTxt();
        
          var src=(text.match('src="(.*?)"')[1]);
          if(src){
            $("input[name='thumb']").val(src);
            $("#thumb").remove();
            $("input[name='thumb']").parents(".layui-form-item").prepend('<img src="'+src+'" id="thumb" class="content-photos">');
          }
      }
    }); 

    form.on('select(select)', function(data){
      $("input[name='url']").val("");
    }); 

    layer.photos({
      photos: 'div'
      ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    }); 
    

	});

  function delimg($this){
      layer.confirm('确定清除这个图片吗？', function(index){
        var sthis=$($this).parent(".layui-form-item");
        var src=sthis.find("img").attr("src");
        {if condition="config('site.file_del' eq '1')"}
        $.post("{:url('common/removefile')}",{"file":src},function(){});
        {/if}
        //清除img dom
        sthis.remove();
        
        layer.close(index);
      });  
  }
  function delfiledata(){
    layer.confirm('确定清除这个文件吗？', function(index){

        $("#filedata").html("");
        
        layer.close(index);
      });
  }
</script>



{/block}